package com.example.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.gulimallproduct.entity.AttrAttrgroupRelationEntity;
import com.example.gulimallproduct.request.AttrAttrGroupRelationRequest;
import com.example.gulimallproduct.service.AttrAttrgroupRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimallproduct.entity.AttrGroupEntity;
import com.example.gulimallproduct.service.AttrGroupService;
import com.example.common.utils.R;



/**
 * 属性分组
 *
 * @author zzl
 * @email zzl@gmail.com
 * @date 2025-10-24 15:22:38
 */
@RestController
@RequestMapping("/attrgroup")
@Slf4j
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 查询未关联的属性
     */
    @GetMapping("{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId, @RequestParam Map<String, Object> params){
        log.info("查询属性分组未关联的属性:{}", params);
        return attrGroupService.attrNoRelation(params, attrGroupId);
    }

    @GetMapping("{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){

        log.info("查询属性分组关联的属性:{}", attrGroupId);
        return attrGroupService.attrRelation(attrGroupId);
    }
    /**
     * 列表
     */
    @GetMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId){
        log.info("查询属性分组列表:{}", params);
//        PageUtils page = attrGroupService.queryPage(params);

        return attrGroupService.selectPage(params, catId);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        log.info("信息:{}", attrGroupId);

        return attrGroupService.getInfoById(attrGroupId);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    @PostMapping("attr/relation")
    public R attrRelation(@RequestBody List<AttrAttrGroupRelationRequest> list){
        log.info("新增属性和属性分组关联关系:{}", list);
        return attrGroupService.SaveRelation(list);
    }

    @PostMapping("attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrAttrGroupRelationRequest> list){
        log.info("删除属性和属性分组关联关系:{}", list);
        attrAttrgroupRelationService.removeBatchByIds(list);
        return attrGroupService.delRelation(list);
    }

    @GetMapping("{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        log.info("查询属性分组和属性:{}", catelogId);
        return attrGroupService.getAttrGroupWithAttrs(catelogId);
    }

}
